package com.init.system.dingding;

import com.aliyun.dingtalkim_1_0.Client;
import com.aliyun.dingtalkim_1_0.models.SendRobotInteractiveCardHeaders;
import com.aliyun.dingtalkim_1_0.models.SendRobotInteractiveCardRequest;
import com.aliyun.dingtalkim_1_0.models.SendRobotInteractiveCardResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

public class Sample {
    public static void main(String[] args) throws Exception {
        new Sample().sendDingCard();
    }
    public void sendDingCard() throws Exception {
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        Client client = new Client(config);
        SendRobotInteractiveCardHeaders sendRobotInteractiveCardHeaders = new SendRobotInteractiveCardHeaders();
        sendRobotInteractiveCardHeaders.xAcsDingtalkAccessToken = "594642f771ac35e3b08403ce0c387c82";
        SendRobotInteractiveCardRequest.SendRobotInteractiveCardRequestSendOptions sendOptions = new SendRobotInteractiveCardRequest.SendRobotInteractiveCardRequestSendOptions()
                .setAtUserListJson("[{\"nickName\":\"南瓜\",\"userId\":\"17288120684188960\"}]")
                .setAtAll(false)
                .setReceiverListJson("[{\"userId\":\"17288120684188960\"}]");
        SendRobotInteractiveCardRequest sendRobotInteractiveCardRequest = new SendRobotInteractiveCardRequest()
                //固定填写：
                .setCardTemplateId("StandardCard")
                //群会话id
                .setSingleChatReceiver("{\"userId\":\"17288120684188960\"}")
                //发送不同卡片内容，需传入不同的cardBizId。
                .setCardBizId("dingCard003")
                //机器人ID，当该机器人为企业内部开发-机器人时，填写机器人应用的appKey
                //其他机器人填写机器人的robotCode值
                .setRobotCode("dinga2ea3xdsjndrglob")
                .setCardData("{\n" +
                        "  \"config\": {\n" +
                        "    \"autoLayout\": true,\n" +
                        "    \"enableForward\": true\n" +
                        "  },\n" +
                        "  \"header\": {\n" +
                        "    \"title\": {\n" +
                        "      \"type\": \"text\",\n" +
                        "      \"text\": \"工单系统通知\"\n" +
                        "    },\n" +
                        "    \"logo\": \"@lALPDfJ6V_FPDmvNAfTNAfQ\"\n" +
                        "  },\n" +
                        "  \"contents\": [\n" +
                        "    {\n" +
                        "      \"type\": \"divider\",\n" +
                        "      \"id\": \"divider_1730769734554\"\n" +
                        "    },\n" +
                        "    {\n" +
                        "      \"type\": \"image\",\n" +
                        "      \"image\": \"@lADPDetfgIhc5KbNCHDNDwA\",\n" +
                        "      \"id\": \"image_1730769734554\"\n" +
                        "    },\n" +
                        "    {\n" +
                        "      \"type\": \"section\",\n" +
                        "      \"fields\": {\n" +
                        "        \"list\": [\n" +
                        "          {\n" +
                        "            \"type\": \"text\",\n" +
                        "            \"text\": \"钉钉发起“C10圆桌派”，旨在邀请各行各业的CIO、CTO等，面对面深入交流数字化建设心得，总结行业…\",\n" +
                        "            \"id\": \"text_1730769734618\"\n" +
                        "          }\n" +
                        "        ]\n" +
                        "      },\n" +
                        "      \"extra\": {\n" +
                        "        \"type\": \"button\",\n" +
                        "        \"label\": {\n" +
                        "          \"type\": \"text\",\n" +
                        "          \"text\": \"查看详情\",\n" +
                        "          \"id\": \"text_1730769734578\"\n" +
                        "        },\n" +
                        "        \"actionType\": \"openLink\",\n" +
                        "        \"url\": {\n" +
                        "          \"all\": \"dingtalk://dingtalkclient/action/openapp?corpid=ding4c066df496aaaa6c35c2f4657eb6378f&container_type=work_platform&app_id=0_bc20632e-6863-482d-9034-1a6ea7fbae2c&redirect_type=jump&redirect_url=http://101.132.133.59:8088\"\n" +
                        "        },\n" +
                        "        \"status\": \"primary\",\n" +
                        "        \"id\": \"button_1646816886531\"\n" +
                        "      },\n" +
                        "      \"id\": \"section_1730769734554\"\n" +
                        "    }\n" +
                        "  ]\n" +
                        "}")
                .setSendOptions(sendOptions);
        try {
            SendRobotInteractiveCardResponse sendRobotInteractiveCardResponse = client.sendRobotInteractiveCardWithOptions(sendRobotInteractiveCardRequest, sendRobotInteractiveCardHeaders, new RuntimeOptions());
            System.out.println(sendRobotInteractiveCardResponse.getBody().toString());
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
                System.out.println(err.code);
                System.out.println(err.message);
            }
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
                System.out.println(err.code);
                System.out.println(err.message);
            }
        }
    }
}
